package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.TLowairServiceFile;
import com.ruoyi.system.dto.req.SysNoticeDto;
import com.ruoyi.system.dto.req.TLowairServiceFileDto;
import com.ruoyi.system.dto.resq.SysNoticeRespDto;
import com.ruoyi.system.mapper.SysNoticeMapper;
import com.ruoyi.system.mapper.TLowairServiceFileMapper;
import com.ruoyi.system.service.SysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dell
 * @description 针对表【sys_notice(通知公告表)】的数据库操作Service实现
 * @createDate 2023-11-09 14:52:30
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice>
        implements SysNoticeService {

    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    @Autowired
    private TLowairServiceFileMapper tLowairServiceFileMapper;

    @Override
    public SysNoticeRespDto selectNoticeById(String noticeId) {
        SysNoticeRespDto sysNoticeRespDto = new SysNoticeRespDto();
        SysNotice sysNotice = sysNoticeMapper.selectById(noticeId);
        BeanUtils.copyBeanProp(sysNoticeRespDto, sysNotice);

        TLowairServiceFile tLowairServiceFile = tLowairServiceFileMapper.selectOne(new LambdaQueryWrapper<TLowairServiceFile>().eq(TLowairServiceFile::getBusinessId, noticeId));
        TLowairServiceFileDto tLowairServiceFileDto = new TLowairServiceFileDto();
        BeanUtils.copyBeanProp(tLowairServiceFileDto, tLowairServiceFile);
        sysNoticeRespDto.setTLowairServiceFileDto(tLowairServiceFileDto);

        return sysNoticeRespDto;
    }

    @Override
    public List<SysNoticeRespDto> selectNoticeList(String noticeTitle) {

        return sysNoticeMapper.selectListByNoticeTitle(noticeTitle);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertNotice(SysNoticeDto sysNoticeDto) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyBeanProp(sysNotice, sysNoticeDto);
        sysNotice.setCreateTime(DateUtils.getNowDate());
        sysNotice.setCreateBy(SecurityUtils.getUsername());
        sysNotice.setUpdateTime(DateUtils.getNowDate());
        sysNotice.setUpdateBy(SecurityUtils.getUsername());

        if (sysNotice.getStatus() == 6) {
            if (sysNotice.getNoticeReleaseTime().after(DateUtils.getNowDate())) {
                //1-待发布
                sysNotice.setStatus(1);
            } else if (sysNotice.getNoticeReleaseTime().before(DateUtils.getNowDate()) || sysNotice.getNoticeReleaseTime().equals(DateUtils.getNowDate())) {
                //0-已发布
                sysNotice.setStatus(0);
            }
        }

        sysNoticeMapper.insert(sysNotice);

        TLowairServiceFileDto file = sysNoticeDto.getFile();
        TLowairServiceFile tLowairServiceFile = new TLowairServiceFile();
        BeanUtils.copyBeanProp(tLowairServiceFile, file);
        tLowairServiceFile.setBusinessId(sysNotice.getNoticeId());
        tLowairServiceFile.setCreateTime(file.getCreateTime());
        tLowairServiceFile.setCreateBy(SecurityUtils.getUsername());
        tLowairServiceFile.setUpdateTime(DateUtils.getNowDate());
        tLowairServiceFile.setUpdateBy(SecurityUtils.getUsername());
        tLowairServiceFileMapper.insert(tLowairServiceFile);

        return AjaxResult.success("新增成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateNotice(SysNoticeDto sysNoticeDto) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyBeanProp(sysNotice, sysNoticeDto);
        sysNotice.setUpdateTime(DateUtils.getNowDate());
        sysNotice.setUpdateBy(SecurityUtils.getUsername());

        if (sysNotice.getStatus() == 6) {
            if (sysNotice.getNoticeReleaseTime().after(DateUtils.getNowDate())) {
                //1-待发布
                sysNotice.setStatus(1);
            } else if (sysNotice.getNoticeReleaseTime().before(DateUtils.getNowDate()) || sysNotice.getNoticeReleaseTime().equals(DateUtils.getNowDate())) {
                //0-已发布
                sysNotice.setStatus(0);
            }
        }

        sysNoticeMapper.updateById(sysNotice);


        TLowairServiceFileDto file = sysNoticeDto.getFile();
        TLowairServiceFile tLowairServiceFile = new TLowairServiceFile();
        BeanUtils.copyBeanProp(tLowairServiceFile, file);
        tLowairServiceFile.setBusinessId(sysNotice.getNoticeId());
        //如果file_id为null则新增
        if (StringUtils.isNull(file.getFileId())) {
            tLowairServiceFile.setBusinessId(sysNotice.getNoticeId());
            tLowairServiceFile.setCreateTime(file.getCreateTime());
            tLowairServiceFile.setCreateBy(SecurityUtils.getUsername());
            tLowairServiceFile.setUpdateTime(DateUtils.getNowDate());
            tLowairServiceFile.setUpdateBy(SecurityUtils.getUsername());
            tLowairServiceFileMapper.insert(tLowairServiceFile);
        } else {
            tLowairServiceFile.setUpdateTime(DateUtils.getNowDate());
            tLowairServiceFile.setUpdateBy(SecurityUtils.getUsername());
            tLowairServiceFileMapper.updateById(tLowairServiceFile);
        }


        return AjaxResult.success("修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult deleteNoticeByIds(List<String> noticeIds) {

        for (String noticeId : noticeIds) {
            tLowairServiceFileMapper.delete(new LambdaQueryWrapper<TLowairServiceFile>().eq(TLowairServiceFile::getBusinessId, noticeId));
            sysNoticeMapper.deleteById(noticeId);
        }

        return AjaxResult.success("删除成功");
    }
}




